package edu.unindra.raport.gui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import edu.unindra.raport.dao.entity.Guru;
import edu.unindra.raport.dao.impl.GuruImpl;

import javax.swing.JComboBox;
import javax.swing.JTextArea;

public class FormDetailMenuGuruEdit extends JDialog {

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;
	private JButton btnUbah = null;
	private JButton btnBatal = null;

	private FormDetailMenuGuru parent;

	private JLabel lblNip;
	private JTextField txtNip;

	private JLabel lblTeTgl;
	private JLabel lblTaLahir;
	private JTextField txtTeTgl;
	private JTextField txtTaTgl;

	private GuruImpl siImpl = null;

	private JLabel lblNama;
	private JTextField txtNama;
	@SuppressWarnings("rawtypes")
	private JComboBox cbJbtn;
	@SuppressWarnings("rawtypes")
	private JComboBox cbPndkn;
	private JLabel lblJbtn;
	private JLabel lblPndkn;
	private JLabel lblBdgStudy;
	@SuppressWarnings("rawtypes")
	private JComboBox cbBdgStudy;
	private JLabel lblAlamat;
	private JTextArea textArea;

	public FormDetailMenuGuruEdit(FormDetailMenuGuru parent) {
		this.parent = parent;

		siImpl = new GuruImpl();

		setModal(true);
		initialize();
	}

	private void initialize() {
		setDefaultCloseOperation(2);
		setTitle("Ubah Data Guru");
		setContentPane(getJContentPane());

		java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit()
				.getScreenSize();
		setBounds((screenSize.width - 560) / 2, (screenSize.height - 418) / 2,
				560, 324);
	}

	private JPanel getJContentPane() {
		if (this.jContentPane == null) {
			GridBagConstraints gbc_lblNip = new GridBagConstraints();
			gbc_lblNip.gridx = 1;
			gbc_lblNip.anchor = GridBagConstraints.WEST;
			gbc_lblNip.insets = new Insets(2, 2, 5, 5);
			gbc_lblNip.weightx = 0.0D;
			gbc_lblNip.gridy = 0;
			this.lblNip = new JLabel();
			lblNip.setHorizontalAlignment(SwingConstants.LEFT);
			this.lblNip.setText("Nip");
			this.jContentPane = new JPanel();
			GridBagLayout gbl_jContentPane = new GridBagLayout();
			gbl_jContentPane.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0, 0,
					0, 0 };
			gbl_jContentPane.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0,
					0.0, 0.0, 0.0, 1.0, 0.0, 0.0 };
			gbl_jContentPane.columnWeights = new double[] { 0.0, 0.0, 0.0, 1.0,
					0.0, 0.0 };
			this.jContentPane.setLayout(gbl_jContentPane);
			GridBagConstraints gbc_txtNip = new GridBagConstraints();
			gbc_txtNip.gridwidth = 3;
			gbc_txtNip.insets = new Insets(0, 0, 5, 5);
			gbc_txtNip.fill = GridBagConstraints.HORIZONTAL;
			gbc_txtNip.gridx = 3;
			gbc_txtNip.gridy = 0;
			jContentPane.add(getTxtCardNumber(), gbc_txtNip);
			this.jContentPane.add(this.lblNip, gbc_lblNip);
			GridBagConstraints gbc_lblNama = new GridBagConstraints();
			gbc_lblNama.anchor = GridBagConstraints.WEST;
			gbc_lblNama.insets = new Insets(0, 0, 5, 5);
			gbc_lblNama.gridx = 1;
			gbc_lblNama.gridy = 1;
			jContentPane.add(getLblNama(), gbc_lblNama);
			GridBagConstraints gbc_txtNama = new GridBagConstraints();
			gbc_txtNama.gridwidth = 3;
			gbc_txtNama.insets = new Insets(0, 0, 5, 5);
			gbc_txtNama.fill = GridBagConstraints.HORIZONTAL;
			gbc_txtNama.gridx = 3;
			gbc_txtNama.gridy = 1;
			jContentPane.add(getTxtNama(), gbc_txtNama);
			GridBagConstraints gbc_lblTeTgl = new GridBagConstraints();
			gbc_lblTeTgl.anchor = GridBagConstraints.WEST;
			gbc_lblTeTgl.insets = new Insets(0, 0, 5, 5);
			gbc_lblTeTgl.gridx = 1;
			gbc_lblTeTgl.gridy = 2;
			jContentPane.add(getLblTeTgl(), gbc_lblTeTgl);
			GridBagConstraints gbc_txtTeTgl = new GridBagConstraints();
			gbc_txtTeTgl.gridwidth = 3;
			gbc_txtTeTgl.insets = new Insets(0, 0, 5, 5);
			gbc_txtTeTgl.fill = GridBagConstraints.HORIZONTAL;
			gbc_txtTeTgl.gridx = 3;
			gbc_txtTeTgl.gridy = 2;
			jContentPane.add(getTxtTeTgl(), gbc_txtTeTgl);
			GridBagConstraints gbc_lblTaLahir = new GridBagConstraints();
			gbc_lblTaLahir.anchor = GridBagConstraints.WEST;
			gbc_lblTaLahir.insets = new Insets(0, 0, 5, 5);
			gbc_lblTaLahir.gridx = 1;
			gbc_lblTaLahir.gridy = 3;
			jContentPane.add(getLblTaLahir(), gbc_lblTaLahir);
			GridBagConstraints gbc_txtTaTgl = new GridBagConstraints();
			gbc_txtTaTgl.gridwidth = 3;
			gbc_txtTaTgl.insets = new Insets(0, 0, 5, 5);
			gbc_txtTaTgl.fill = GridBagConstraints.HORIZONTAL;
			gbc_txtTaTgl.gridx = 3;
			gbc_txtTaTgl.gridy = 3;
			jContentPane.add(getTxtTaTgl(), gbc_txtTaTgl);
			GridBagConstraints gbc_lblJbtn = new GridBagConstraints();
			gbc_lblJbtn.anchor = GridBagConstraints.WEST;
			gbc_lblJbtn.insets = new Insets(0, 0, 5, 5);
			gbc_lblJbtn.gridx = 1;
			gbc_lblJbtn.gridy = 4;
			jContentPane.add(getLblJbtn(), gbc_lblJbtn);
			GridBagConstraints gbc_cbJbtn = new GridBagConstraints();
			gbc_cbJbtn.gridwidth = 3;
			gbc_cbJbtn.insets = new Insets(0, 0, 5, 5);
			gbc_cbJbtn.fill = GridBagConstraints.HORIZONTAL;
			gbc_cbJbtn.gridx = 3;
			gbc_cbJbtn.gridy = 4;
			jContentPane.add(getCbJbtn(), gbc_cbJbtn);
			GridBagConstraints gbc_lblPndkn = new GridBagConstraints();
			gbc_lblPndkn.anchor = GridBagConstraints.WEST;
			gbc_lblPndkn.insets = new Insets(0, 0, 5, 5);
			gbc_lblPndkn.gridx = 1;
			gbc_lblPndkn.gridy = 5;
			jContentPane.add(getLblPndkn(), gbc_lblPndkn);
			GridBagConstraints gbc_cbPndkn = new GridBagConstraints();
			gbc_cbPndkn.gridwidth = 3;
			gbc_cbPndkn.insets = new Insets(0, 0, 5, 5);
			gbc_cbPndkn.fill = GridBagConstraints.HORIZONTAL;
			gbc_cbPndkn.gridx = 3;
			gbc_cbPndkn.gridy = 5;
			jContentPane.add(getCbPndkn(), gbc_cbPndkn);
			GridBagConstraints gbc_lblBdgStudy = new GridBagConstraints();
			gbc_lblBdgStudy.anchor = GridBagConstraints.WEST;
			gbc_lblBdgStudy.insets = new Insets(0, 0, 5, 5);
			gbc_lblBdgStudy.gridx = 1;
			gbc_lblBdgStudy.gridy = 6;
			jContentPane.add(getLblBdgStudy(), gbc_lblBdgStudy);
			GridBagConstraints gbc_cbBdgStudy = new GridBagConstraints();
			gbc_cbBdgStudy.gridwidth = 3;
			gbc_cbBdgStudy.insets = new Insets(0, 0, 5, 5);
			gbc_cbBdgStudy.fill = GridBagConstraints.HORIZONTAL;
			gbc_cbBdgStudy.gridx = 3;
			gbc_cbBdgStudy.gridy = 6;
			jContentPane.add(getCbBdgStudy(), gbc_cbBdgStudy);
			GridBagConstraints gbc_lblAlamat = new GridBagConstraints();
			gbc_lblAlamat.anchor = GridBagConstraints.WEST;
			gbc_lblAlamat.insets = new Insets(0, 0, 5, 5);
			gbc_lblAlamat.gridx = 1;
			gbc_lblAlamat.gridy = 7;
			jContentPane.add(getLblAlamat(), gbc_lblAlamat);
			GridBagConstraints gbc_textArea = new GridBagConstraints();
			gbc_textArea.gridheight = 2;
			gbc_textArea.gridwidth = 3;
			gbc_textArea.insets = new Insets(0, 0, 5, 5);
			gbc_textArea.fill = GridBagConstraints.BOTH;
			gbc_textArea.gridx = 3;
			gbc_textArea.gridy = 7;
			jContentPane.add(getTextArea(), gbc_textArea);
			GridBagConstraints gbc_btnBatal = new GridBagConstraints();
			gbc_btnBatal.insets = new Insets(0, 0, 0, 5);
			gbc_btnBatal.gridx = 5;
			gbc_btnBatal.gridy = 9;
			jContentPane.add(getBtnBatal(), gbc_btnBatal);
			GridBagConstraints gbc_btnUbah = new GridBagConstraints();
			gbc_btnUbah.gridx = 6;
			gbc_btnUbah.insets = new Insets(2, 2, 2, 0);
			gbc_btnUbah.anchor = 13;
			gbc_btnUbah.gridy = 9;
			this.jContentPane.add(getBtnUbah(), gbc_btnUbah);
		}
		return this.jContentPane;
	}

	private JButton getBtnUbah() {
		if (this.btnUbah == null) {
			this.btnUbah = new JButton();
			this.btnUbah.setText("Ubah");
			this.btnUbah.setToolTipText("Insert");
			this.btnUbah.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					int retVal = 0;
					Guru si = new Guru();

					si.setNip(txtNip.getText());
					si.setNama(txtNama.getText());
					si.setTeTgl(txtTeTgl.getText());
					si.setTaTgl(txtTaTgl.getText());
					si.setJbtn(cbJbtn.getSelectedItem().toString());
					si.setPndkn(cbPndkn.getSelectedItem().toString());
					si.setBidStudy(cbBdgStudy.getSelectedItem().toString());
					si.setAlamat(textArea.getText());

					try {
						retVal = siImpl.update(si);
					} catch (SQLException e1) {
						e1.printStackTrace();
					}

					if (retVal > -1) {
						JOptionPane.showMessageDialog(
								FormDetailMenuGuruEdit.this,
								"Proses update data guru  berhasil");
						int i = parent.getTable().convertRowIndexToModel(
								parent.getTable().getSelectedRow());

						parent.getModelTable().setGuruAt(si, i);

						FormDetailMenuGuruEdit.this.setVisible(false);
					} else {
						JOptionPane.showMessageDialog(
								FormDetailMenuGuruEdit.this,
								"Proses update data guru  tidak berhasil");
					}
				}
			});
		}
		return this.btnUbah;
	}

	JTextField getTxtCardNumber() {
		if (txtNip == null) {
			txtNip = new JTextField();
			txtNip.setColumns(10);

			txtNip.setEnabled(false);
		}
		return txtNip;
	}

	private JButton getBtnBatal() {
		if (btnBatal == null) {
			btnBatal = new JButton();
			btnBatal.setToolTipText("Insert");
			btnBatal.setText("Batal");
			btnBatal.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					FormDetailMenuGuruEdit.this.setVisible(false);
				}
			});
		}
		return btnBatal;
	}

	private JLabel getLblTeTgl() {
		if (lblTeTgl == null) {
			lblTeTgl = new JLabel();
			lblTeTgl.setText("Tempat Lahir");
		}
		return lblTeTgl;
	}

	private JLabel getLblTaLahir() {
		if (lblTaLahir == null) {
			lblTaLahir = new JLabel();
			lblTaLahir.setText("Tanggal Lahir");
		}
		return lblTaLahir;
	}

	private JTextField getTxtTeTgl() {
		if (txtTeTgl == null) {
			txtTeTgl = new JTextField();
			txtTeTgl.setColumns(10);
		}
		return txtTeTgl;
	}

	private JTextField getTxtTaTgl() {
		if (txtTaTgl == null) {
			txtTaTgl = new JTextField();
			txtTaTgl.setEnabled(false);
			txtTaTgl.setColumns(10);
		}
		return txtTaTgl;
	}

	public void setGuru(Guru e) {
		if (e == null) {
			throw new IllegalArgumentException();
		}

		txtNip.setText(e.getNip());
		txtNama.setText(e.getNama());
		txtTeTgl.setText(e.getTeTgl());
		txtTaTgl.setText(e.getTaTgl());
		cbJbtn.getModel().setSelectedItem(e.getJbtn());
		cbPndkn.getModel().setSelectedItem(e.getPndkn());
		cbBdgStudy.getModel().setSelectedItem(e.getBidStudy());
		textArea.setText(e.getAlamat());
	}

	private JLabel getLblNama() {
		if (lblNama == null) {
			lblNama = new JLabel();
			lblNama.setText("Nama Guru");
			lblNama.setHorizontalAlignment(SwingConstants.LEFT);
		}
		return lblNama;
	}

	private JTextField getTxtNama() {
		if (txtNama == null) {
			txtNama = new JTextField();
			txtNama.setColumns(10);
		}
		return txtNama;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private JComboBox getCbJbtn() {
		if (cbJbtn == null) {
			cbJbtn = new JComboBox();
			cbJbtn.setModel(new DefaultComboBoxModel(new String[] { "KepSek",
					"WaKepSek", "Guru" }));
		}
		return cbJbtn;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private JComboBox getCbPndkn() {
		if (cbPndkn == null) {
			cbPndkn = new JComboBox();
			cbPndkn.setModel(new DefaultComboBoxModel(new String[] { "SLTA",
					"Diploma", "Strata 1", "Strata 2" }));
		}
		return cbPndkn;
	}

	private JLabel getLblJbtn() {
		if (lblJbtn == null) {
			lblJbtn = new JLabel();
			lblJbtn.setText("Jabatan");
		}
		return lblJbtn;
	}

	private JLabel getLblPndkn() {
		if (lblPndkn == null) {
			lblPndkn = new JLabel();
			lblPndkn.setText("Pendidikan");
		}
		return lblPndkn;
	}

	private JLabel getLblBdgStudy() {
		if (lblBdgStudy == null) {
			lblBdgStudy = new JLabel();
			lblBdgStudy.setText("Bidang Study");
		}
		return lblBdgStudy;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private JComboBox getCbBdgStudy() {
		if (cbBdgStudy == null) {
			cbBdgStudy = new JComboBox();
			cbBdgStudy.setModel(new DefaultComboBoxModel(new String[] {
					"Bahasa Indonesia", "Bahasa Inggris", "Matematika",
					"Ilmu Pengetahuan Alam", "Ilmu Pengetahuan Sosial" }));
		}
		return cbBdgStudy;
	}

	private JLabel getLblAlamat() {
		if (lblAlamat == null) {
			lblAlamat = new JLabel();
			lblAlamat.setText("Alamat");
		}
		return lblAlamat;
	}

	private JTextArea getTextArea() {
		if (textArea == null) {
			textArea = new JTextArea();
		}
		return textArea;
	}
}